package fm.dongman.animefm.view.adapter

import android.content.Context
import android.support.v4.view.PagerAdapter
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup

/**
 * 新番速递页面适配器
 * Created by shize on 2017/10/23.
 */
class ComicCourierPagerAdapter constructor(private val mContext: Context,
                                           private val mTitles: List<String>,
                                           private val mRecyclers: List<RecyclerView>)
    : PagerAdapter() {

    override fun isViewFromObject(view: View?, `object`: Any?): Boolean = view == `object`

    override fun getCount(): Int = mTitles.size

    override fun getPageTitle(position: Int): CharSequence = mTitles[position]

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {
        if (position > mRecyclers.size) {
            return mRecyclers.last()
        }
        val recycler = mRecyclers[position]
        recycler.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        container!!.addView(recycler)
        return recycler
    }

    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container!!.removeView(`object` as View?)
    }
}